<?php
/**
 * 微博后台管理项目 媒体表数据层

 */
define("CACHE_ALBUM_INFO","album_info_");
class db_album_info extends db_base
{
	var $table_name  ="album_info";
	
	function get_album_item($id)
	{
		$id = intval($id);
		$key = CACHE_ALBUM_INFO . "-get_album_item-{$id}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where = " id = '{$id}' and status =1  ";
			$content = $this->get_alllist($this->table_name,1,1, "", $where);
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}

		return $content[0];
	}
	
	function get_album_item_by_topic_id_is_tuijian($topic_id,$show_type=0)
	{
		$topic_id = intval($topic_id);
		$show_type = intval($show_type);
		$key = CACHE_ALBUM_INFO . "-get_album_item_by_topic_id_is_tuijian-{$topic_id}-{$show_type}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			if($show_type)$where_ary[] = "mblog_show_type = {$show_type}";
			if($topic_id)$where_ary[] = "mblog_topic_id = {$topic_id}";
			$where_ary[] = "is_tuijian = 1";
			$where_ary[] = "status = 1";
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,1,100,"",$where);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		$count = count($content);
		
		return $content[rand(0,$count-1)];
	}
	
	
	function get_album_index_count($show_type="",$topic_id="",$uid="")
	{
		$topic_id = intval($topic_id);
		$show_type = intval($show_type);
		$uid = intval($uid);
		
		$key = CACHE_ALBUM_INFO . "-get_album_index_count-{$show_type}-{$topic_id}-{$uid}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			if($show_type)$where_ary[] = " mblog_show_type ={$show_type}  ";
			if($topic_id)$where_ary[] = " mblog_topic_id ={$topic_id}  ";
			if($uid)$where_ary[] = " create_uid ={$uid}  ";
			$where_ary[] = " show_index =1  ";
			$where_ary[] = " status =1  ";
			$where = join(" and ",$where_ary);
			$content = $this->get_listcount($this->table_name,$where);
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}

		return $content;
	}
	
	function get_album_index_list_order_by_hot($p=1,$count=20,$show_type="",$topic_id="",$uid="",$count_status="")
	{
		$topic_id = intval($topic_id);
		$show_type = intval($show_type);
		$uid = intval($uid);
		$count = intval($count);
		$p = intval($p);
		$count_status = intval($count_status);
		
		$key = CACHE_ALBUM_INFO . "-get_album_index_list_order_by_hot-{$p}-{$count}-{$show_type}-{$topic_id}-{$uid}-{$count_status}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			if($show_type)$where_ary[] = " mblog_show_type ={$show_type}  ";
			if($topic_id)
			{
				$where_ary[] = " mblog_topic_id ={$topic_id}  ";
			}
			else
			{
				$where_ary[] = " mblog_show_type in (1,2,3)  ";
			}
			if($uid)$where_ary[] = " create_uid ={$uid}  ";
			$where_ary[] = " show_index =1  ";
			$where_ary[] = " status =1  ";
			if($count_status) $where_ary[] = " count_status >  $count_status  ";
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$count, "is_tuijian desc , count_tuijian desc", $where);
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}

		return $content;
	}
	
	function get_album_index_list_order_by_time($p=1,$count=20,$show_type="",$topic_id="",$uid="")
	{
		$topic_id = intval($topic_id);
		$show_type = intval($show_type);
		$uid = intval($uid);
		$count = intval($count);
		$p = intval($p);
		
		$key = CACHE_ALBUM_INFO . "-get_album_index_list_order_by_time-{$p}-{$count}-{$show_type}-{$topic_id}-{$uid}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			if($show_type)$where_ary[] = " mblog_show_type ={$show_type}  ";
			if($topic_id)$where_ary[] = " mblog_topic_id ={$topic_id}  ";
			if($uid)$where_ary[] = " create_uid ={$uid}  ";
			$where_ary[] = " show_index =1  ";
			$where_ary[] = " status =1  ";
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$count, "update_time desc", $where);
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}

		return $content;
	}
	
	
	function get_album_index_list_order_by_count_good($p=1,$count=20,$show_type="",$topic_id="",$uid="")
	{
		$topic_id = intval($topic_id);
		$show_type = intval($show_type);
		$uid = intval($uid);
		$count = intval($count);
		$p = intval($p);
		
		$key = CACHE_ALBUM_INFO . "-get_album_index_list_order_by_count_good-{$p}-{$count}-{$show_type}-{$topic_id}-{$uid}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			if($show_type)$where_ary[] = " mblog_show_type ={$show_type}  ";
			if($topic_id)$where_ary[] = " mblog_topic_id ={$topic_id}  ";
			if($uid)$where_ary[] = " create_uid ={$uid}  ";
			$where_ary[] = " show_index =1  ";
			$where_ary[] = " status =1  ";
			$where = join(" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$count, "count_good desc", $where);
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}

		return $content;
	}
	
	
	/**
	 * 根据D_ID取数据
	 * @param $d_id
	 */
	function get_album_list_by_uid($uid,$p=1,$count=20)
	{
		$uid = intval($uid);
		$count = intval($count);
		$p = intval($p);
		
		$key = CACHE_ALBUM_INFO . "-get_album_list_by_uid-{$uid}-{$p}-{$count}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where = " create_uid = '{$uid}' and status =1  ";
			$content = $this->get_alllist($this->table_name,$p,$count, "update_time desc", $where);
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}

		return $content;
	}
	
	/**
	 * 根据D_ID取数据
	 * @param $d_id
	 */
	function get_album_count_by_uid($uid)
	{
		
		$uid = intval($uid);
		
		$key = CACHE_ALBUM_INFO . "-get_album_count_by_uid-{$uid}";
		$mc = mem_cache::get_instance();
		$cache = $mc->get($key);
		if ($cache === false)
		{
			$where = " create_uid = '{$uid}' and status =1  ";
			$content = $this->get_listcount($this->table_name,$where);
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}

		return $content;
	}
	
	
	

	function insert_album($title,$desc,$uid,$show_type,$body_url="",$body_attach="",$show_order="",$mblog_show_type="",$mblog_topic_id="",$mblog_topic="",$create_nickname="")
	{
		$ary["status"] = 1;
		$ary["title"] = $title;
		$ary["desc"] = $desc;
		$ary["create_uid"] = $uid;
		$ary["create_nickname"] = $create_nickname;
		$ary["update_uid"] = $uid;
		$ary["mblog_show_type"] = $show_type;
		$ary["body_url"] = $body_url;
		$ary["body_attach"] = $body_attach;
		$ary["show_order"] = $show_order;
		$ary["create_time"] = Date::get_date_time();
		$ary["create_ip"] = IP::get_client_ip_long();
		$ary["update_time"] = Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();
		
		$ary["mblog_show_type"] = $mblog_show_type;
		$ary["mblog_topic_id"] = $mblog_topic_id;
		$ary["mblog_topic"] = $mblog_topic;
		
		$ret = $this->insert($this->table_name, $ary );
		
		
			
		$mc = mem_cache::get_instance();
		$key[] = CACHE_ALBUM_INFO . "-get_album_list_by_uid-{$uid}-1-10";
		$key[] = CACHE_ALBUM_INFO . "-get_album_count_by_uid-{$uid}";
		$mc->delete_ary($key);
		
		
		return $this->get_lastinsertid();
		
	}
	
	
	function update_album_info_by_id($id,$uid="",$title="",$desc="",$pic1="",$pic2="",$pic3="",$pic4="",$status_count="",$status="",$show_type="",$body_url="",$body_attach="",$id1="",$id2="",$id3="",$id4="",$count_love="",$count_reply="",$count_good="",$level="",$show_order="",$mblog_show_type="",$mblog_topic_id="",$mblog_topic="",$user_nickname="",$update_time="")
	{
		$where = "id = $id";
		
		if(strlen($uid)>0)$ary["update_uid"] = $uid;
		if(strlen($title)>0)$ary["title"] = $title;
		if(strlen($desc)>0)$ary["desc"] = $desc;
		if(strlen($pic1)>0)$ary["pic1"] = $pic1;
		if(strlen($pic2)>0)$ary["pic2"] = $pic2;
		if(strlen($pic3)>0)$ary["pic3"] = $pic3;
		if(strlen($pic4)>0)$ary["pic4"] = $pic4;
		
		if(strlen($id1)>0)$ary["id1"] = $id1;
		if(strlen($id2)>0)$ary["id2"] = $id2;
		if(strlen($id3)>0)$ary["id3"] = $id3;
		if(strlen($id4)>0)$ary["id4"] = $id4;
		
		if(strlen($status_count)>0)$ary["count_status"] = $status_count;
		if(strlen($status)>0)$ary["status"] = $status;
		if(strlen($show_type)>0)$ary["mblog_show_type"] = $show_type;
		if(strlen($body_url)>0)$ary["body_url"] = $body_url;
		if(strlen($body_attach)>0)$ary["body_attach"] = $body_attach;
		if(strlen(level)>0)$ary["level"] = $level;
		if(strlen($show_order)>0)$ary["show_order"] = $show_order;
		if(strlen($mblog_topic_id)>0)$ary["mblog_topic_id"] = $mblog_topic_id;
		if(strlen($mblog_topic)>0)$ary["mblog_topic"] = $mblog_topic;
		if(strlen($mblog_show_type)>0)$ary["mblog_show_type"] = $mblog_show_type;
		if(strlen($user_nickname)>0)$ary["create_nickname"] = $user_nickname;
		
		$count_tuijian = 0;
		if($count_love)
		{
			$ary["count_love"] = "count_love+{$count_love}";
			$ary["count_good"] = "count_good+{$count_love}";//每周清空一次
			$count_tuijian = $count_tuijian + 1;
		}
		if($count_reply)
		{
			$ary["count_reply"] = "count_reply+{$count_reply}";
		}
		/*if($count_good)
		{
			$ary["count_good"] = "count_good+{$count_good}";
			$count_tuijian = $count_tuijian + 1;
		}*/
		
		if($count_tuijian)
		{
			$ary["count_tuijian"] = "count_tuijian+{$count_tuijian}";
		}
		
		
		if($status_count)
		{
			if($status_count >= 4)
			{
				$ary["show_index"] = 	1;

			}
			else
			{
				$ary["show_index"] = 	0;
				
			}
			
			
		}
		$ary["update_time"] = $update_time? $update_time: Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();

		
		$ret = $this->update($this->table_name, $ary ,$where);
		
		$item = $this->get_album_item($id);
		$topic_id = $item["topic_id"];
		$uid = $item["create_uid"];
		
		$mc = mem_cache::get_instance();
		$key[] = CACHE_ALBUM_INFO . "-get_album_item-{$id}";
		$key[] = CACHE_ALBUM_INFO . "-get_album_list_by_uid-{$uid}-1-10";
		$key[] = CACHE_ALBUM_INFO . "-get_album_count_by_uid-{$uid}";
		$mc->delete_ary($key);
		
		
		return $ret;
	}
	
	
	
	
   function get_user_order_by_count_tuijian_nocache($p,$pcount)
	{
		$count = intval($count);
		$p = intval($p);
		
		$where = "status = 1";
		$content = $this->get_alllist($this->table_name,$p,$pcount,"count_tuijian desc,update_time desc ",$where);
		
		return $content ;
	}
	
	
	function admin_get_count_by_search($status="",$title="",$show_index="",$count_status="",$is_tuijian="",$topic_id="")
	{
		$where= array();
		if(strlen($show_index) > 0)
		{
			$where[] = "show_index = {$show_index}";
		}
		if(strlen($status) > 0)
		{
			$status = intval($status);
			$where[] = "status = {$status}";
		}
		if($count_status)
		{
			$where[] = " count_status >= $count_status ";
		}
		if($is_tuijian)
		{
			$where[] = " is_tuijian= $is_tuijian ";
		}
		
		if($title)
		{
			$where[] = "title like '%{$title}%'";
		}
		if($topic_id)
		{
			$where[] = " mblog_topic_id= $topic_id ";
		}
		
		$where= join(" and ",$where);
		
		$data = $this->get_listcount($this->table_name, $where);
		
		return $data;
	}
	
	
	/*
	 * cron获取topic_lisy
	 */
	function admin_get_list_by_search($status="",$p=1,$pcount=10,$title="",$show_index="",$count_status="",$is_tuijian="",$topic_id="")
	{
		$where= array();
		
		if(strlen($show_index) > 0)
		{
			$where[] = "show_index = {$show_index}";
		}
		if(strlen($status) > 0)
		{
			$status = intval($status);
			$where[] = "status = {$status}";
		}
		if($title)
		{
			$where[] = "title like '%{$title}%'";
		}
		if($count_status)
		{
			$where[] = " count_status >= $count_status ";
		}
		if($is_tuijian)
		{
			$where[] = " is_tuijian= $is_tuijian ";
		}
		if($topic_id)
		{
			$where[] = " mblog_topic_id= $topic_id ";
		}
		$where= join(" and ",$where);
		
		$p = intval($p);
		$pcount = intval($pcount);
		
		
		$data = $this->get_alllist($this->table_name,$p,$pcount,"create_time desc",$where);
		
		return $data;
	}
	
	
	
		
	function admin_get_item($id)
	{
		$where = " id = '{$id}'  ";
		$content = $this->get_alllist($this->table_name,1,1, "", $where);
		
		return $content[0];
	}
	
	
	function admin_update_by_id($id,$status="")
	{
		$where = "id = $id";
		$ary = array();

		if(strlen($status)>0)$ary["status"] = $status;
		$ret = $this->update($this->table_name, $ary,$where);
		
		return $ret;
	}
	
	function update_is_tuijian_by_id($id,$is_tuijian="")
	{
		$where = "id = $id";
		$ary = array();

		if(strlen($is_tuijian)>0)$ary["is_tuijian"] = $is_tuijian;
		$ret = $this->update($this->table_name, $ary,$where);
		
		return $ret;
	}
	
	
	function cron_get_by_uid_and_topic_id($uid,$topic_id)
	{
		$where = "create_uid = $uid and mblog_topic_id = $topic_id and is_system=1";
		$content = $this->get_alllist($this->table_name,1,1,"id",$where);
		
		return $content[0];
	}
	
	function cron_insert_album($title,$desc,$uid,$show_type,$body_url="",$body_attach="",$show_order="",$mblog_show_type="",$mblog_topic_id="",$mblog_topic="",$create_nickname="",$pic1="",$create_time="")
	{
		$ary["status"] = 1;
		$ary["title"] = $title;
		$ary["desc"] = $desc;
		$ary["create_uid"] = $uid;
		$ary["create_nickname"] = $create_nickname;
		$ary["update_uid"] = $uid;
		$ary["mblog_show_type"] = $show_type;
		$ary["body_url"] = $body_url;
		$ary["body_attach"] = $body_attach;
		$ary["show_order"] = $show_order;
		$ary["create_time"] =  $create_time ?  $create_time : Date::get_date_time();
		$ary["create_ip"] = IP::get_client_ip_long();
		$ary["update_time"] = $create_time ?  $create_time : Date::get_date_time();
		$ary["update_ip"] = IP::get_client_ip_long();
		
		$ary["pic1"] = $pic1;
		
		$ary["mblog_show_type"] = $mblog_show_type;
		$ary["mblog_topic_id"] = $mblog_topic_id;
		$ary["mblog_topic"] = $mblog_topic;
		$ary["is_system"] = 1;
		
		$ret = $this->insert($this->table_name, $ary );
		
		return $this->get_lastinsertid();
		
	}
	
	
	
	function cron_get_album_list_is_system($p,$pcount,$id)
	{
		$id = intval($id);
		$where = " id > $id and is_system=1 and status = 1";
		$list = $this->get_alllist($this->table_name,$p,$pcount,"id",$where);
		
		return $list;
	}
	
	
	function cron_get_album_count_is_system($id)
	{
		$id = intval($id);
		$where = " id > $id and is_system=1 and status = 1";
		$count = $this->get_listcount($this->table_name,$where);
		
		return $count;
		
	}
	
}
